<template>
  <div class="dashboard-container">
    <div class="dashboard-header">
      <h1>仪表盘</h1>
      <div class="header-info">
        <span>{{ welcomeText }}</span>
        <el-date-picker
          v-model="currentDate"
          type="date"
          format="YYYY年MM月DD日"
          value-format="YYYY-MM-DD"
          size="small"
          :disabled="true"
          class="date-picker"
        />
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalUsers }}</div>
            <div class="stat-label">用户总数</div>
          </div>
          <div class="stat-icon user-icon">
            <el-icon><component is="i-user" /></el-icon>
          </div>
        </div>
        <div class="stat-footer">
          <span
            class="stat-change"
            :class="{ positive: stats.usersChange > 0 }"
          >
            <el-icon v-if="stats.usersChange > 0"
              ><component is="i-arrow-up"
            /></el-icon>
            <el-icon v-else><component is="i-arrow-down" /></el-icon>
            {{ Math.abs(stats.usersChange) }}%
          </span>
          <span class="stat-compare">较上月</span>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalDrones }}</div>
            <div class="stat-label">无人机总数</div>
          </div>
          <div class="stat-icon drone-icon">
            <el-icon><component is="i-plane" /></el-icon>
          </div>
        </div>
        <div class="stat-footer">
          <span
            class="stat-change"
            :class="{ positive: stats.dronesChange > 0 }"
          >
            <el-icon v-if="stats.dronesChange > 0"
              ><component is="i-arrow-up"
            /></el-icon>
            <el-icon v-else><component is="i-arrow-down" /></el-icon>
            {{ Math.abs(stats.dronesChange) }}%
          </span>
          <span class="stat-compare">较上月</span>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-value">{{ stats.activeDrones }}</div>
            <div class="stat-label">在线无人机</div>
          </div>
          <div class="stat-icon active-icon">
            <el-icon><component is="i-video-camera" /></el-icon>
          </div>
        </div>
        <div class="stat-footer">
          <span
            class="stat-change"
            :class="{ positive: stats.activeDronesChange > 0 }"
          >
            <el-icon v-if="stats.activeDronesChange > 0"
              ><component is="i-arrow-up"
            /></el-icon>
            <el-icon v-else><component is="i-arrow-down" /></el-icon>
            {{ Math.abs(stats.activeDronesChange) }}%
          </span>
          <span class="stat-compare">较昨日</span>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-value">{{ stats.totalRoutes }}</div>
            <div class="stat-label">航线总数</div>
          </div>
          <div class="stat-icon route-icon">
            <el-icon><component is="i-location" /></el-icon>
          </div>
        </div>
        <div class="stat-footer">
          <span
            class="stat-change"
            :class="{ positive: stats.routesChange > 0 }"
          >
            <el-icon v-if="stats.routesChange > 0"
              ><component is="i-arrow-up"
            /></el-icon>
            <el-icon v-else><component is="i-arrow-down" /></el-icon>
            {{ Math.abs(stats.routesChange) }}%
          </span>
          <span class="stat-compare">较上月</span>
        </div>
      </el-card>
    </div>

    <!-- 图表区域 -->
    <div class="charts-area">
      <el-card class="chart-card" shadow="hover">
        <template #header>
          <div class="chart-header">
            <span>无人机活动统计</span>
            <el-select v-model="chartRange" size="small" class="chart-range">
              <el-option label="最近7天" value="7d" />
              <el-option label="最近30天" value="30d" />
              <el-option label="最近90天" value="90d" />
            </el-select>
          </div>
        </template>
        <div class="chart-content">
          <!-- 这里可以放置图表组件 -->
          <div class="chart-placeholder">
            <el-icon class="chart-icon-large"
              ><component is="i-pie-chart"
            /></el-icon>
            <p>无人机活动统计图表</p>
          </div>
        </div>
      </el-card>

      <el-card class="chart-card" shadow="hover">
        <template #header>
          <div class="chart-header">
            <span>任务完成情况</span>
            <el-select v-model="taskRange" size="small" class="chart-range">
              <el-option label="本周" value="week" />
              <el-option label="本月" value="month" />
              <el-option label="本季度" value="quarter" />
            </el-select>
          </div>
        </template>
        <div class="chart-content">
          <!-- 这里可以放置图表组件 -->
          <div class="chart-placeholder">
            <el-icon class="chart-icon-large"
              ><component is="i-trend-charts"
            /></el-icon>
            <p>任务完成情况图表</p>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 最近活动 -->
    <el-card class="recent-activities" shadow="hover">
      <template #header>
        <div class="chart-header">
          <span>最近活动</span>
          <el-button type="text" size="small" @click="refreshActivities">
            <el-icon><component is="i-refresh" /></el-icon>
            刷新
          </el-button>
        </div>
      </template>
      <el-table :data="recentActivities" style="width: 100%">
        <el-table-column prop="time" label="时间" width="180" />
        <el-table-column prop="user" label="用户" width="120" />
        <el-table-column prop="action" label="操作" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'success' ? 'success' : 'info'">
              {{ row.status === "success" ? "成功" : "进行中" }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted } from "vue";
import { ElMessage } from "element-plus";
import service from '@/utils/request';

export default {
  name: "Dashboard",
  setup() {
    const currentDate = ref("");
    const chartRange = ref("7d");
    const taskRange = ref("week");
    const loading = ref(false);

    // 统计数据
    const stats = reactive({
      totalUsers: 0,
      usersChange: 0,
      totalDrones: 0,
      dronesChange: 0,
      activeDrones: 0,
      activeDronesChange: 0,
      totalRoutes: 0,
      routesChange: 0,
    });

    // 最近活动
    const recentActivities = ref([]);

    // 欢迎文本
    const welcomeText = computed(() => {
      const hour = new Date().getHours();
      if (hour < 12) return "早上好！";
      if (hour < 18) return "下午好！";
      return "晚上好！";
    });

    // 获取仪表盘数据
    const fetchDashboardData = async () => {
      loading.value = true;
      try {
        const response = await service.get("/statistics/dashboard");

        if (response.code === 200 && response.data) {
          // 更新统计数据
          Object.assign(stats, response.data.stats);

          // 更新最近活动
          recentActivities.value = response.data.recentActivities;
        } else {
          ElMessage.error("获取仪表盘数据失败");
        }
      } catch (error) {
        console.error("获取仪表盘数据失败:", error);
        ElMessage.error("获取仪表盘数据失败");
      } finally {
        loading.value = false;
      }
    };

    // 刷新活动
    const refreshActivities = () => {
      fetchDashboardData();
    };

    onMounted(() => {
      // 设置当前日期
      const now = new Date();
      currentDate.value = now.toISOString().split("T")[0];

      // 获取仪表盘数据
      fetchDashboardData();
    });

    return {
      currentDate,
      chartRange,
      taskRange,
      stats,
      recentActivities,
      welcomeText,
      refreshActivities,
      loading,
    };
  },
};
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.dashboard-header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}

.header-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.date-picker {
  width: 180px;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  height: 150px;
  transition: transform 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 32px;
  font-weight: 600;
  color: #303133;
}

.stat-label {
  font-size: 14px;
  color: #606266;
  margin-top: 5px;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  font-size: 24px;
}

.user-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.drone-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.active-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.route-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #909399;
}

.stat-change {
  display: flex;
  align-items: center;
  gap: 4px;
}

.stat-change.positive {
  color: #67c23a;
}

.charts-area {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.chart-card {
  height: 300px;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-range {
  width: 120px;
}

.chart-content {
  height: 240px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.chart-placeholder {
  text-align: center;
  color: #909399;
}

.chart-icon-large {
  font-size: 48px;
  margin-bottom: 10px;
  opacity: 0.5;
}

.recent-activities {
  margin-bottom: 30px;
}

@media (max-width: 768px) {
  .charts-area {
    grid-template-columns: 1fr;
  }

  .stats-cards {
    grid-template-columns: 1fr;
  }
}
</style>
